package cn.com.headfree.evaluation.service.impl;

import cn.com.headfree.entity.TDeclareEvaluationInfo;
import cn.com.headfree.entity.TRecordOptLog;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.evaluation.dao.IDeclareEvaluationDao;
import cn.com.headfree.evaluation.service.IDeclareEvaluationService;
import cn.com.headfree.recordlog.service.IRecordLogService;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.IdGen;
import com.gta.edu.sdk.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: TODO
 * @Author wcSun
 * @Date 2022/6/28
 **/
@Service("declareEvaluationService")
public class DeclareEvaluationServiceImpl implements IDeclareEvaluationService {

    @Autowired
    private IDeclareEvaluationDao declareEvaluationDao;

    @Autowired
    private IRecordLogService recordLogService;

    @Override
    public void saveDeclareEvaluationInfo(TDeclareEvaluationInfo declareEvaluationInfo) {
        declareEvaluationInfo.setCreaterId(declareEvaluationInfo.getErpNo());
        declareEvaluationInfo.setCreaterName(declareEvaluationInfo.getUserName());
        declareEvaluationInfo.setUpdaterId(declareEvaluationInfo.getErpNo());
        declareEvaluationInfo.setUpdaterName(declareEvaluationInfo.getUserName());
        declareEvaluationInfo.setCreateTime(new Date());
        declareEvaluationInfo.setUpdateTime(new Date());
        declareEvaluationInfo.setDelSign(0);
        declareEvaluationInfo.setAuditStatus("0");//待审核

        if(StringUtils.isEmpty(declareEvaluationInfo.getId())){
            declareEvaluationInfo.setId(IdGen.uuid());
        }
        declareEvaluationDao.saveDeclareEvaluationInfo(declareEvaluationInfo);
    }

    @Override
    public List<TDeclareEvaluationInfo> judge(TDeclareEvaluationInfo tDeclareEvaluationInfo) {
        return declareEvaluationDao.judge(tDeclareEvaluationInfo);
    }

    @Override
    public void getPageDeclareEvaluationInfo(Page<TDeclareEvaluationInfo> page, String year, User user) {
        declareEvaluationDao.getPageTDeclareEvaluationInfo(page, year, user);
    }

    @Override
    public void auditAgreeInfo(String declareEvaluationId, User user) {
        //先修改专家人才申报表状态，由待审核变更为同意
        declareEvaluationDao.updateDeclareEvaluationState(declareEvaluationId,"1",null);
        //日志
        TRecordOptLog log = new TRecordOptLog();
        log.setOptType("6");
        log.setOptContent(user.getName()+"核实并同意了该人才评价申请");
        log.setCurrentRecordId(declareEvaluationId);
        log.setRecordId(declareEvaluationId);
        recordLogService.save(user,log);
    }

    @Override
    public void auditRejectInfo(String declareEvaluationId, User user,String auditOpt) {
        //先修改薪档申报状态--由待审核变更为不同意
        declareEvaluationDao.updateDeclareEvaluationState(declareEvaluationId,"2",auditOpt);
        //删除积分信息
//        DeclareEvaluationDao.deletePersonYearPointDetailInfo(DeclareEvaluationId);
        //日志
        TRecordOptLog log = new TRecordOptLog();
        log.setOptType("8");
        if(StringUtil.isNullOrEmpty(auditOpt)){
            log.setOptContent(user.getName()+"驳回了该人才评价申请");
        }else{
            log.setOptContent(user.getName()+"驳回了该人才评价申请，驳回理由是："+auditOpt);
        }
        log.setCurrentRecordId(declareEvaluationId);
        log.setRecordId(declareEvaluationId);
        recordLogService.save(user,log);
    }

    @Override
    public void getPageDeclareEvaluationInfoQuery(Page<TDeclareEvaluationInfo> page, String baseOrgId,  String deptId,String userName, String erpNo, Integer year, String auditStatus){
        declareEvaluationDao.getPageDeclareEvaluationInfoQuery(page,baseOrgId,deptId,userName,erpNo,year,auditStatus);
    }

    @Override
    public List<TDeclareEvaluationInfo> getDeclareEvaluationInfoQuery(String baseOrgId, String deptId,String userName, String erpNo, Integer year, String auditStatus){
        return declareEvaluationDao.getListSalaryInfoQuery(baseOrgId, deptId,userName,erpNo,year,auditStatus);
    }

    @Override
    public TDeclareEvaluationInfo getTDeclareEvaluationInfoById(String id) {
        return declareEvaluationDao.getTDeclareEvaluationInfoById(id);
    }
}
